trigger trg_PassportHistory on Passport_History__c (after insert, before update) 
{

	//This Map Will Hold (AccountId , Passport Id)
    Map<Id,Id> passportObjectMap = new Map<Id,Id>();
    Map<Id,Id> passportObjectInsertMap = new Map<Id,Id>();
   
    //Get all Accounts which will be updated with Current Passport Id
    if(Trigger.isInsert)
    {
    	for(Passport_History__c passObject : Trigger.new)
	    {
	    	System.debug('The Passport Object is Current Value => '+ passObject.isCurrent__c);
	    	if(passObject.isCurrent__c == true)
            {
                passportObjectInsertMap.put(passObject.Passport_Holder__c , passObject.id);  
                  
            }	
	    }
    }
    else if(Trigger.isUpdate)
    {
    	for(Passport_History__c passObject : Trigger.new)
	    {
	        if(Trigger.oldmap.get(passObject.id).iscurrent__c != passObject.isCurrent__c)
	        {
	            if(passObject.isCurrent__c == true)
	            {
	                passportObjectMap.put(passObject.Passport_Holder__c , passObject.id);  
	              
	            }
	        }
	        
	    }	
    }
	
	
	if(passportObjectInsertMap.size() > 0)
    {
    	//Get old Passport which is isCurrent = true and mark it as false
		//List<Passport_History__c> oldPassportList = new List<Passport_History__c>([select isCurrent__c from Passport_History__c where Passport_Holder__c = :accountId and isCurrent__c = true  ]);
		List<Passport_History__c> oldPassportList = new List<Passport_History__c>();
    
  		for (Passport_History__c passObject : [select isCurrent__c,Passport_Holder__c from Passport_History__c where Passport_Holder__c IN :passportObjectInsertMap.keyset() and isCurrent__c = true  ] )
  		{
        	if(passObject.Id != passportObjectInsertMap.get(passObject.Passport_Holder__c))
			{
				passObject.isCurrent__c = false ;
	        	//Adding in the List...
	        	oldPassportList.add(passObject);
			}
        	
  		}	
  		
        //Query all Accounts with Selected Ids and Update Current Passport 
        //List<Account> accountList = new List<Account>([select Passport_History_Number__c from Account where id in : accountIds]);
        List<Contact> contactList = new List<Contact>([select Passport_Number__c from Contact where id in : passportObjectInsertMap.keyset()]);
        for (Contact conObject : contactList){
           conObject.Passport_Number__c = passportObjectInsertMap.get(conObject.id);
        }
        
        if(oldPassportList.size() > 0)
        	update oldPassportList;
        
        if (contactList.size() > 0 )
           update contactList;
    		
    }
    
    
	//For Updating Current passport....
    if(passportObjectMap.size()> 0)
    {
		
		//Get old Passport which is isCurrent = true and mark it as false
		//List<Passport_History__c> oldPassportList = new List<Passport_History__c>([select isCurrent__c from Passport_History__c where Passport_Holder__c = :accountId and isCurrent__c = true  ]);
		List<Passport_History__c> oldPassportList = new List<Passport_History__c>();
    
  		for (Passport_History__c passObject : [select isCurrent__c from Passport_History__c where Passport_Holder__c IN :passportObjectMap.keyset() and isCurrent__c = true  ] )
  		{
        	passObject.isCurrent__c = false ;
        	//Adding in the List...
        	oldPassportList.add(passObject);
  		}	
  		
        //Query all Accounts with Selected Ids and Update Current Passport 
        //List<Account> accountList = new List<Account>([select Passport_History_Number__c from Account where id in : accountIds]);
        List<Contact> contactList = new List<Contact>([select Passport_Number__c from Contact where id in : passportObjectMap.keyset()]);
        for (Contact conObject : contactList){
           conObject.Passport_Number__c = passportObjectMap.get(conObject.id);
        }
        
        if(oldPassportList.size() > 0)
        	update oldPassportList;
        
        if (contactList.size() > 0 )
           update contactList;
        
        
        
    }  


}